package com.jade.editor.anisotropy.parts;

import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.ResourceManager;
import org.eclipse.wb.swt.SWTResourceManager;

import com.jade.editor.anisotropy.editors.AnisotropyEditor;
import com.jade.editor.anisotropy.editors.AnisotropyModel;
import com.jade.editor.anisotropy.node.Collector;

public class ToolBarSide {
	
	public final static int TOOL_SELECT = 0;
	public final static int TOOL_POINT = 1;
	public final static int TOOL_LINE = 2;
	
	private DrawArea _drawArea;
	private boolean _showGridFlag = false;
	private boolean _showBGImgFlag = false;
	private boolean _showRulerFlag = false;
	private boolean _showSettingsTabFlag = false;
	private int _metricGrid = DrawArea.METRIC_PX;
	private IPath _filePath;
	private String _selectedImageName = null;
	private int _toolSelected = TOOL_SELECT;
	private AnisotropyModel _model = null;
	private AnisotropyEditor _editor = null;

	
	public ToolBarSide(Composite parent, DrawArea drawArea, IPath filePath, AnisotropyModel model, AnisotropyEditor editor){
		createPartControl(parent);
		_model = model;
		_editor = editor;
		_filePath = filePath;
		
		_drawArea = drawArea;
		
		if(_drawArea != null)
			_drawArea.initArea();
		else{
			_drawArea.setParent(parent);
			_drawArea.initArea();
			_drawArea.setEditor(_editor);
			_drawArea.setModel(_model);
		}
//		
//		if(_objModel.getBGColor() != null){
//			_drawArea.setBGColor(_objModel.getBGColor());
//			_drawArea.setBGStatus(DrawArea.BACKGROUND_COLOR);
//		}
//		if(_objModel.getBGImagePath() != null && !_objModel.getBGImagePath().equals("") && _objModel.getBGImageLocation() != null){
//			_drawArea.setBGImageLocation(_objModel.getBGImageLocation());
//			File img = new File(_objModel.getBGImagePath());
//			_drawArea.setBGImage(img);
//			_selectedImageName = img.getName();
//			_drawArea.setBGStatus(DrawArea.BACKGROUND_IMAGE);
//		}
		
	}
	
	public Collector getCollector(){
		return _drawArea.getCollector();
	}

	public void addToolBar(Composite parent, DrawArea drawArea){
		createPartControl(parent);
	}
	
	
	
	private void createPartControl(Composite parent){
		final Composite toolBarLayer = new Composite(parent, SWT.NONE);
		toolBarLayer.setBounds(0, 0, 26, 300);
		toolBarLayer.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		toolBarLayer.setLayout(null);
		
		//------------------------------------
		//-----------Show Bars----------------
		//------------------------------------
		ToolBar toolBarShowGrid = new ToolBar(toolBarLayer, SWT.WRAP | SWT.RIGHT | SWT.VERTICAL);
		toolBarShowGrid.setBounds(0, 0, 24, 22);
				
		final ToolItem itemShowGrid = new ToolItem(toolBarShowGrid, SWT.RADIO);
		itemShowGrid.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_GRID.png"));
		itemShowGrid.setToolTipText("Show Grid");
		itemShowGrid.setSelection(_showGridFlag);
		itemShowGrid.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(_showGridFlag) {
					_showGridFlag = false;
					_drawArea.showGrid(false);
				}
				else {
					_showGridFlag = true;
					_drawArea.showGrid(true);
				}
						
				itemShowGrid.setSelection(_showGridFlag);
			}
		});
				
		//------------------------------------
				
		ToolBar toolBarShowRuler = new ToolBar(toolBarLayer, SWT.WRAP | SWT.RIGHT | SWT.VERTICAL);
		toolBarShowRuler.setBounds(0, 22, 24, 22);
		
		final ToolItem itemShowRuler = new ToolItem(toolBarShowRuler, SWT.RADIO);
		itemShowRuler.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_RULER.png"));
		itemShowRuler.setToolTipText("Show Ruler");
		itemShowRuler.setSelection(_showRulerFlag);
		itemShowRuler.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(_showRulerFlag) {
					_showRulerFlag = false;
					_drawArea.setRulerVisible(false);
				}
				else {
					_showRulerFlag = true;
					_drawArea.setRulerVisible(true);
				}
				
				itemShowRuler.setSelection(_showRulerFlag);
			}
		});
		
		
		ToolBar toolBarShowBG = new ToolBar(toolBarLayer, SWT.WRAP | SWT.RIGHT | SWT.VERTICAL);
		toolBarShowBG.setBounds(0, 44, 24, 22);
		
		final ToolItem itemShowBG = new ToolItem(toolBarShowBG, SWT.RADIO);
		itemShowBG.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_BG.png"));
		itemShowBG.setToolTipText("Show Background Image");
		itemShowBG.setSelection(_showBGImgFlag);
		itemShowBG.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				_showBGImgFlag = _showBGImgFlag ? false : true;
				_drawArea.showBGImage(_showBGImgFlag);
					
				itemShowBG.setSelection(_showBGImgFlag);
			}
		});
		
		//------------------------------------
		//-----------Settings Bar-------------
		//------------------------------------
		ToolBar toolBarEditor = new ToolBar(toolBarLayer, SWT.FLAT | SWT.RIGHT | SWT.VERTICAL);
		toolBarEditor.setBounds(0, 69, 24, 66);
		
		ToolItem itemGridSettings = new ToolItem(toolBarEditor, SWT.PUSH);
		itemGridSettings.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_GRID_SETTINGS.png"));
		itemGridSettings.setToolTipText("Grid Settings");
		itemGridSettings.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				SettingsGrid sg = new SettingsGrid(toolBarLayer.getShell(), _drawArea.getGridDistanse(), _drawArea.getGridColor(), _metricGrid);
				sg.open();
				if(sg.getReturnCode() == Window.OK){
					_metricGrid = sg.getMetrics();
					_drawArea.setGridColor(sg.getColor());
					_drawArea.setGridDistanse(sg.getDistanse());
				}
				
			}
		});
		
		ToolItem itemBGimageSettings = new ToolItem(toolBarEditor, SWT.PUSH);
		itemBGimageSettings.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_BG_SETTINGS.png"));
		itemBGimageSettings.setToolTipText("Background settings");
		itemBGimageSettings.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				SettingsBG sBG = new SettingsBG(
						toolBarLayer.getShell(),
						_filePath,
						_drawArea.getBGStatus(),
						_drawArea.getDrawAreaSize(),
						_drawArea.getGridDistanse(),
						_drawArea.getBGColor(),
						_drawArea.getBGImageLocation(),
						_selectedImageName
						);
				sBG.open();
				
				if (sBG.getReturnCode() == Window.OK){
					if (sBG.getBGStatus() == DrawArea.BACKGROUND_COLOR){
						_drawArea.setBGColor(sBG.getBGColor());
					}
					if (sBG.getBGStatus() == DrawArea.BACKGROUND_IMAGE){
						_drawArea.setBGImageLocation(sBG.getImgLocation());
						_drawArea.setBGImage(sBG.getBGImage());
						_selectedImageName = sBG.getSelectedImageName();
					}
//					_editor.setDirty(true);
					_drawArea.setBGStatus(sBG.getBGStatus());
				}
				
				
			}
		});
		
		ToolItem itemAniSettings = new ToolItem(toolBarEditor, SWT.PUSH);
		itemAniSettings.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_ANISOTROPY _SETTINGS.png"));
		itemAniSettings.setToolTipText("Anisotropy Settings");
		itemAniSettings.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				SettingAni sa = new SettingAni(toolBarLayer.getShell(), _drawArea.getDrawAreaSize(), _drawArea.getCollector().getBorderColor(), _drawArea.getCollector().getSelectionColor(), _drawArea.getCollector().getConnectionColor(), _drawArea.getCollector());
				sa.open();
				//TODO
				if (sa.getReturnCode() == Window.OK){
					if (sa.getSize() != null){
						_drawArea.setDrawAreaSize(sa.getSize());
						_drawArea.getCollector().clear();
					}
				}
			}
		});

		
		//------------------------------------
		//-----------Select Bar---------------
		//------------------------------------
		ToolBar toolBarSelect = new ToolBar(toolBarLayer, SWT.FLAT | SWT.RIGHT | SWT.VERTICAL);
		toolBarSelect.setBounds(0, 139, 24, 66);
		
		ToolItem itemSelectTool = new ToolItem(toolBarSelect, SWT.RADIO);
		itemSelectTool.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_POINTER.png"));
		itemSelectTool.setToolTipText("Selection tool");
		itemSelectTool.setSelection(true);
		itemSelectTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showSelectToolLayer();
			}
		});
		
		ToolItem itemPointTool = new ToolItem(toolBarSelect, SWT.RADIO);
		itemPointTool.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_POINT.png"));
		itemPointTool.setToolTipText("Point tool");
		itemPointTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showPointToolLayer();
			}
		});
		
		ToolItem itemLineTool = new ToolItem(toolBarSelect, SWT.RADIO);
		itemLineTool.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_LINE.png"));
		itemLineTool.setToolTipText("Line tool");
		itemLineTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showLineToolLayer();
			}
		});
		
		//------------------------------------
		//-----------Zoom Bar-----------------
		//------------------------------------
		ToolBar toolBarZoom = new ToolBar(toolBarLayer, SWT.FLAT | SWT.RIGHT | SWT.VERTICAL);
		toolBarZoom.setBounds(0, 208, 24, 88);
		
		ToolItem itemZoomIn = new ToolItem(toolBarZoom, SWT.PUSH);
		itemZoomIn.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_ZOOM_IN.png"));
		itemZoomIn.setToolTipText("Zoom In");
		itemZoomIn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
			}
		});
		
		ToolItem itemZoomOut = new ToolItem(toolBarZoom, SWT.PUSH);
		itemZoomOut.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_ZOOM_OUT.png"));
		itemZoomOut.setToolTipText("Zoom Out");
		itemZoomOut.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
			}
		});
		
		ToolItem itemZoomFit = new ToolItem(toolBarZoom, SWT.PUSH);
		itemZoomFit.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_ZOOM_FIT.png"));
		itemZoomFit.setToolTipText("Fit to Editor");
		itemZoomFit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
			}
		});
		
		ToolItem itemZoom100 = new ToolItem(toolBarZoom, SWT.PUSH);
		itemZoom100.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_ZOOM_100.png"));
		itemZoom100.setToolTipText("Zoom to 100%");
		
		//------------------------------------
		//-----------Separators---------------
		//------------------------------------
		
		Label separatorH1 = new Label(toolBarLayer, SWT.SEPARATOR | SWT.HORIZONTAL);
		separatorH1.setBounds(0, 67, 26, 2);
		
		Label separatorH2 = new Label(toolBarLayer, SWT.SEPARATOR | SWT.HORIZONTAL);
		separatorH2.setLocation(0, 137);
		separatorH2.setSize(26, 2);
		
		Label separatorH3 = new Label(toolBarLayer, SWT.SEPARATOR | SWT.HORIZONTAL);
		separatorH3.setBounds(0, 206, 26, 2);
		
		Label separatorV1 = new Label(toolBarLayer, SWT.SEPARATOR | SWT.VERTICAL);
		separatorV1.setLocation(25, 0);
		separatorV1.setSize(2, 370);

		
		
		addToolSettingsLayer(parent);

	}
	
	
	
	
	
	
	
	
	
	
	
	private Composite _selectAddLayer = null;
	private Composite _lineAddLayer = null;
	private Composite _pointAddLayer = null;
	
	private Button _btnSelectionTool = null;
	
	private Button _btnConnectPoints = null;
	private Button _btnDisconnectPoints = null;
	
	private Button _btnAddPoint = null;
	private Button _btnRemovePoint = null;
	
	private Label _titleLabel = null;
	
	private static final int LINE_TOOL_CONNECT = 0;
	private static final int LINE_TOOL_DISCONNECT = 1;
	private int _lineToolState = LINE_TOOL_CONNECT;
	
	private static final int POINT_TOOL_ADD = 0;
	private static final int POINT_TOOL_REMOVE = 2;
	private int _pointToolState = POINT_TOOL_ADD;
	
	
	private void showPointToolLayer(){
		_selectAddLayer.setVisible(false);
		_lineAddLayer.setVisible(false);
		_pointAddLayer.setVisible(true);
		_titleLabel.setText("Tool Settings: Point Tool");
		
		switch (_pointToolState){
		case POINT_TOOL_ADD : {
			_btnAddPoint.setSelection(true);
			_btnRemovePoint.setSelection(false);
			
			_drawArea.getCollector().setMode(Collector.MODE_ADD);
			break;
		}
		case POINT_TOOL_REMOVE : {
			_btnAddPoint.setSelection(false);
			_btnRemovePoint.setSelection(true);
			
			_drawArea.getCollector().setMode(Collector.MODE_REMOVE);
			break;
		}
		default : {
			_btnAddPoint.setSelection(true);
			_btnRemovePoint.setSelection(false);
			
			_drawArea.getCollector().setMode(Collector.MODE_ADD);
			break;
		}
		}
	}
	
	private void showSelectToolLayer(){
		_selectAddLayer.setVisible(true);
		_lineAddLayer.setVisible(false);
		_pointAddLayer.setVisible(false);
		
		_drawArea.getCollector().setMode(Collector.MODE_MOVE);
		_titleLabel.setText("Tool Settings: Selection Tool");
	}
	
	private void showLineToolLayer(){
		_selectAddLayer.setVisible(false);
		_lineAddLayer.setVisible(true);
		_pointAddLayer.setVisible(false);
		_titleLabel.setText("Tool Settings: Line Tool");
		
		switch(_lineToolState){
		case LINE_TOOL_CONNECT : {
			_btnConnectPoints.setSelection(true);
			_btnDisconnectPoints.setSelection(false);
			
			_drawArea.getCollector().setMode(Collector.MODE_CONNECT);
			break;
		}
		case LINE_TOOL_DISCONNECT : {
			_btnConnectPoints.setSelection(false);
			_btnDisconnectPoints.setSelection(true);
			
			_drawArea.getCollector().setMode(Collector.MODE_DISCONNECT);
			break;
		}
		default : {
			_btnConnectPoints.setSelection(true);
			_btnDisconnectPoints.setSelection(false);
			
			_drawArea.getCollector().setMode(Collector.MODE_CONNECT);
			break;
		}
		}
	}
	
	private void addToolSettingsLayer(Composite parent){
		//------------------------------------
		//-----------Setting Menu Layer-------
		//------------------------------------
		final Composite settingsLayer = new Composite(parent, SWT.NONE);
		settingsLayer.setBounds(0, 301, 26, 27);
		settingsLayer.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		
		ToolBar toolBar = new ToolBar(settingsLayer, SWT.FLAT | SWT.RIGHT);
		toolBar.setLocation(0, 2);
		toolBar.setSize(24, 24);
		
		final ToolItem dropItem = new ToolItem(toolBar, SWT.NONE);
		dropItem.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_DOWN.png"));
		dropItem.setToolTipText("Show settings bar");
		dropItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Rectangle max = new Rectangle(0, 371, 220, 245);
				Rectangle min = new Rectangle(0, 371, 26, 27);

				
				if(_showSettingsTabFlag){
					_showSettingsTabFlag = false;
					settingsLayer.setSize(settingsLayer.computeSize(min.width, min.height));
					dropItem.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_DOWN.png"));
				}
				else {
					_showSettingsTabFlag = true;
					settingsLayer.setSize(settingsLayer.computeSize(max.width, max.height));
					dropItem.setImage(ResourceManager.getPluginImage("com.jade.editor.object", "icons/IMG_UP.png"));
				}
				
			}
		});
		
		
		
		Label separatorH1m = new Label(settingsLayer, SWT.SEPARATOR | SWT.HORIZONTAL);
		separatorH1m.setBounds(0, 0, 220, 2);
		
		Label separatorH2m = new Label(settingsLayer, SWT.SEPARATOR | SWT.HORIZONTAL);
		separatorH2m.setBounds(0, 243, 220, 2);
		
		Label SeparatorV1m = new Label(settingsLayer, SWT.SEPARATOR | SWT.VERTICAL);
		SeparatorV1m.setBounds(218, 0, 2, 245);
		
		Label separatorV2m = new Label(settingsLayer, SWT.SEPARATOR | SWT.VERTICAL);
		separatorV2m.setBounds(25, 0, 2, 25);
		
		Label separatorH3m = new Label(settingsLayer, SWT.SEPARATOR | SWT.HORIZONTAL);
		separatorH3m.setBounds(0, 26, 220, 2);
		
		_titleLabel = new Label(settingsLayer, SWT.NONE);
		_titleLabel.setBounds(30, 5, 182, 15);
		_titleLabel.setText("Tool Settings:");
		
		//----------------------------------------------------------------------

		_selectAddLayer = new Composite(settingsLayer, SWT.NONE);
		_selectAddLayer.setBounds(2, 28, 214, 212);
		
		_btnSelectionTool = new Button(_selectAddLayer, SWT.RADIO);
		_btnSelectionTool.setEnabled(false);
		_btnSelectionTool.setSelection(true);
		_btnSelectionTool.setBounds(10, 10, 194, 16);
		_btnSelectionTool.setText("Selection Tool");
		
		
		
		
		_lineAddLayer = new Composite(settingsLayer, SWT.NONE);
		_lineAddLayer.setBounds(2, 28, 214, 212);
		
		_btnConnectPoints = new Button(_lineAddLayer, SWT.RADIO);
		_btnConnectPoints.setSelection(true);
		_btnConnectPoints.setBounds(10, 10, 194, 16);
		_btnConnectPoints.setText("Connect Points");
		_btnConnectPoints.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_lineToolState = LINE_TOOL_CONNECT;
				_drawArea.getCollector().setMode(Collector.MODE_CONNECT);
			}
		});
		
		_btnDisconnectPoints = new Button(_lineAddLayer, SWT.RADIO);
		_btnDisconnectPoints.setBounds(10, 32, 194, 16);
		_btnDisconnectPoints.setText("Disconnect Points");
		_btnDisconnectPoints.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_lineToolState = LINE_TOOL_DISCONNECT;
				_drawArea.getCollector().setMode(Collector.MODE_DISCONNECT);
			}
		});
		
		
		
		_pointAddLayer = new Composite(settingsLayer, SWT.NONE);
		_pointAddLayer.setBounds(2, 28, 214, 212);
		
		_btnAddPoint = new Button(_pointAddLayer, SWT.RADIO);
		_btnAddPoint.setSelection(true);
		_btnAddPoint.setBounds(10, 10, 194, 16);
		_btnAddPoint.setText("Add Point");
		_btnAddPoint.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_pointToolState = POINT_TOOL_ADD;
				_drawArea.getCollector().setMode(Collector.MODE_ADD);
			}
		});
		
		_btnRemovePoint = new Button(_pointAddLayer, SWT.RADIO);
		_btnRemovePoint.setBounds(10, 54, 194, 16);
		_btnRemovePoint.setText("Remove Point");
		_btnRemovePoint.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_pointToolState = POINT_TOOL_REMOVE;
			_drawArea.getCollector().setMode(Collector.MODE_REMOVE);
			}
		});
		
		Button btnClear = new Button(_pointAddLayer, SWT.NONE);
		btnClear.setBounds(129, 177, 75, 25);
		btnClear.setText("Clear");
		btnClear.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_drawArea.getCollector().clear();
			}
		});
		
		switch(_toolSelected){
		case TOOL_SELECT : {
			_selectAddLayer.setVisible(true);
			_lineAddLayer.setVisible(false);
			_pointAddLayer.setVisible(false);
			_titleLabel.setText("Tool Settings: Selection Tool");
			break;
		}
		case TOOL_LINE : {
			_selectAddLayer.setVisible(false);
			_lineAddLayer.setVisible(true);
			_pointAddLayer.setVisible(false);
			_titleLabel.setText("Tool Settings: Line Tool");
			break;
			
		}
		case TOOL_POINT : {
			_selectAddLayer.setVisible(false);
			_lineAddLayer.setVisible(false);
			_pointAddLayer.setVisible(true);
			_titleLabel.setText("Tool Settings: Point Tool");
			break;
		}
		default : {
			_selectAddLayer.setVisible(true);
			_lineAddLayer.setVisible(false);
			_pointAddLayer.setVisible(false);
			_titleLabel.setText("Tool Settings: Selection Tool");
			break;
		}
		
		}
	}
}
